/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.lr;

import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;

import edu.strath.cis.grallan.findbugs.adaptiveranking.AbstractAdaptiveRanker;
import edu.strath.cis.grallan.findbugs.adaptiveranking.IRankScheme;
import edu.strath.cis.grallan.findbugs.adaptiveranking.RankCalculatorStrategy;
import edu.strath.cis.grallan.findbugs.adaptiveranking.gui.sort.DefaultSortOrder;
import edu.strath.cis.grallan.findbugs.adaptiveranking.population.PopulationExtractor;
import edu.strath.cis.grallan.findbugs.adaptiveranking.util.BugFromAdaptiveRankComparator;
import edu.strath.cis.grallan.findbugs.adaptiveranking.util.NumberRankScheme;
import edu.strath.cis.grallan.findbugs.adaptiveranking.util.StackedComparatorBuilder;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.cloud.Cloud.UserDesignation;
import edu.umd.cs.findbugs.filter.Matcher;
import edu.umd.cs.findbugs.gui2.IApplicationState;
import edu.umd.cs.findbugs.xml.XMLOutput;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class LearningValueRanker extends AbstractAdaptiveRanker<LearningRank> {

	private final IRankScheme<Double> rankScheme;
	private final Comparator<LearningRank> tieBreakingComparator;

	private final RankCalculatorStrategy learningValueCalculator = new LearningRankCalculator();

	public LearningValueRanker(IApplicationState applicationState, PopulationExtractor populationExtractorPrototype) {
		super(applicationState, populationExtractorPrototype);
		this.rankScheme = new NumberRankScheme<Double>(0.0d, 1.0d, 0.0d);

		this.tieBreakingComparator = new StackedComparatorBuilder<LearningRank>().sortBy(LEARNING_VALUE_COMPARATOR)
				.then(BugFromAdaptiveRankComparator.transformFor(DefaultSortOrder.COMPARATOR)).thenTie();
	}

	private static class UnclassifiedMatcher implements Matcher {
		public boolean match(BugInstance bugInstance) {
			return bugInstance.getUserDesignationKey().equalsIgnoreCase(UserDesignation.UNCLASSIFIED.name());
		}

		public void writeXML(XMLOutput xmlOutput, boolean disabled) throws IOException {
			// Unnecessary till XML is required.
		}

	}

	static final UnclassifiedMatcher UNCLASSIFIED_MATCHER = new UnclassifiedMatcher();

	private static class LearningValueComparator implements Comparator<LearningRank>, Serializable {
		public int compare(LearningRank first, LearningRank second) {
			return Double.compare(first.rank(), second.rank());
		}
	}

	private static final LearningValueComparator LEARNING_VALUE_COMPARATOR = new LearningValueComparator();

	protected void assignRankForBug(PopulationExtractor extractor, BugInstance bug) {
		if (bugAlreadyClassified(bug)) {
			assignBugLearningValue(bug, 0.0d);
			return;
		}

		double learningValue = learningValueCalculator.calculateRank(extractor, bug);

		assignBugLearningValue(bug, learningValue);
	}

	@Override
	protected void createRankScheme() {
		// final rankScheme already created in constructor
	}

	@Override
	protected Comparator<? super LearningRank> rankingComparator() {
		return tieBreakingComparator;
	}

	private void assignBugLearningValue(BugInstance bug, double learningValue) {
		LearningRank bugRank = new LearningRank(bug, learningValue);
		super.assignBugRank(bugRank);
	}

	@SuppressWarnings("unused")
	private double rankEnsuringMagnitude(double highestRank) {
		return highestRank == 0.0d ? 1.0d : highestRank;
	}

	@Override
	protected double relativePositionOf(LearningRank bugRank) {
		double relativePosition = rankScheme.relativePosition(bugRank.rank());
		return relativePosition;
	}

	public double defaultRelativeRank() {
		return rankScheme.relativePosition(rankScheme.getInitial());
	}

}
